class Veryfasttree < Formula
  desc "Efficient phylogenetic tree inference for massive taxonomic datasets"
  homepage "https://github.com/citiususc/veryfasttree"
  url "https://github.com/citiususc/veryfasttree/archive/refs/tags/v4.0.5.tar.gz"
  sha256 "e753c01555b3363747ea1d51248d691aa1e79d228cac187a6725ea8cd86ad321"
  license all_of: [
    "GPL-3.0-only",
    "BSD-3-Clause", # libs/cli11
    "MPL-2.0", # libs/bxzstr
  ]
  head "https://github.com/citiususc/veryfasttree.git", branch: "master"

  bottle do
    sha256 cellar: :any,                 arm64_sequoia: "e377c3426836e11514282dfcd1fc2699fc6c25f721eaae8cf4f2bdf9ea41d0b1"
    sha256 cellar: :any,                 arm64_sonoma:  "bbbb02a03c029e97e52372649ba3ed63176cfd17567eb6a58ae05307bc63db3c"
    sha256 cellar: :any,                 arm64_ventura: "d78b15098c68bd0567765551e2cd6139df15b5e17426768b716ff8a8b9183a80"
    sha256 cellar: :any,                 sonoma:        "b52e607f554c788ee123c30fdd3fe87a0be0cdd175374eee9e9e380866a0ce74"
    sha256 cellar: :any,                 ventura:       "914f6187005dc9c904f502bc865f53beb0b041cd5c93f233c6b84b8688cf3483"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "7b220e9b734bfd7db831ba87a603065222e23d7b72d485c3bfabb309e6117faf"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "f7a8c21c3829da9c21800b2107f95fccc5758bdf2a5c9cbe1546be455bb82b76"
  end

  depends_on "boost" => :build
  depends_on "cmake" => :build
  depends_on "robin-map" => :build
  depends_on "xxhash" => :build

  uses_from_macos "bzip2"
  uses_from_macos "zlib"

  on_macos do
    depends_on "libomp"
  end

  def install
    # remove libraries that can be unbundled
    rm_r(Dir["libs/*"] - ["libs/CLI11", "libs/bxzstr"])

    # workaround to use brew `robin-map` which needs C++17
    inreplace "CMakeLists.txt", "set(CMAKE_CXX_STANDARD 11)", "set(CMAKE_CXX_STANDARD 17)"
    ENV.append_to_cflags "-Wno-register"

    args = ["-DUSE_SHARED=ON"]
    args << "-DUSE_NATIVE=OFF" if ENV.effective_arch != :native
    args << "-DUSE_SEE4=ON" if Hardware::CPU.intel? && OS.mac? && MacOS.version.requires_sse41?

    system "cmake", "-S", ".", "-B", "build", *args, *std_cmake_args
    system "cmake", "--build", "build"
    system "cmake", "--install", "build"
    man1.install "man/VeryFastTree.1"
  end

  test do
    (testpath/"test.fasta").write <<~FASTA
      >N3289
      --RNRSCRRDNTNGQDLQAALAIFAAKVYVGVALQSVQVAAGIGKHPVYKHIPSKKYTGL
      IIQELYLERLMAELADGLADAAPDVLLDIRGLMLALDAPAREKPIIL-LHLAASAGDALR
      DKGQALRRELLPRLSGLGYAGLASGALTGDNATLMSARLIGLLVSATLLAL---------
      -----------------
      >N1763
      ------ISKDTTEERFLEVDKLTFAPKSYAGTLQTKILSAVSVPAGTLYKDFPTTELALL
      VTLEVYQATDTSGAQDGLAANARDILHVLVELFLALAGFAAQDPLHLLLPMAAALTSSLR
      GRLRELRRELLAKGAAKVYTGLGAADATGDGVQLGAASLAMQLLGALLPCLRLDALLGSL
      ASGLPEEKLASLAIFL-
      >N2100
      --RGRARPKQTTAESNLDATMGKFASQEYDGTMHRELGAASGVSLGTLYPDYPTWEMLIL
      VTLESYLEPVVSALYAGLATDAPDILQR-LQLFLALLGFAMNHPGALLKSLAATLESELC
      GKLKALTREVLEKLGASVFEGLPEPTLTGDEATPMSAALLMPLVQALLLCLLLQPLLAKH
      SDDLPQIILAIYGIF--
      >N774
      --RGRRRTKTIVSEKDLSATMGRFAEQPYDGSLERNAATAASAPLNTMYGEFPTQDMFLL
      MCLESYLIPTVLEADAE-ATEARDVLRRRLQLFLALLGFALNHPTQLLKMLATTLHKALR
      GKIKDLQREVFARLTASAPAGLAAQFLTGDNATLMEAVLLMPFLAALLSCLILEPLDRKF
      ADDFPAVILAIYAIF--
      >N211
      --KARGRTTIETGEKVLTGEMDRFAELQYDGSLQRDDTTGAAPPLGTLYGKLPTQDMFLL
      FALESYLDPGTPELGQGLATKAPDGLRKRLHLFLGLLSFSLDHPVHLLKSLATT-HKAVR
      GKVKDLQRDRFARLNASAPSGIAHPALTGDMATLMEAGLLMPLLAALLPILILAPLDKKY
      AHDNHNDILAIYAIFLT
      >N747
      MGKARGITTAYAYSQVLIGRLGAHAALPYNGSLERKDVAALDAPTNKLYGQFPDGDSWLL
      GALEAYIHTCPPELPQSLATQAPETIFTRLQPYLGLADFGLAHPGQLLKIEATKLQRAVR
      GKFKELQKDAPAQLTANGITVVGQPNLTGDLGTLSEAVVLLQLVPSLLAAIIFKPIDKKY
      GESAPVGILLPFSVW--
      >N952
      MGRGRARTTVEAGEKVLLGTMIRFAELPHDGSLQRNDSTALAAPLNTLYAKFPTQDMFLL
      FALESYLHPSSPELGMGLATPAPDILRKRLALFLGLLSFSLEHPIQLLKSLATT-HKAVR
      GPFKDLQKDVPAHLTATAPSGIAHPALTGDMATLMEAVLLMPLLAALLPVLVLKPLDKKF
      ADDSPGDILAVYAIF--
      >N3964
      ------RTTVEDNDKVLNATMDRFADLPYDGSLQRDDTTAQTAPLGTLYGKFPTADMFLL
      NALESYLDPKRPELGQGLATKAPDALRKKLQLFLGLLAFALSHPNRLLKSLATT-HKLVR
      GKLKDQEREIFARLTASAPPAIAHPALTGDMATLMEAVLLMPLLAALLTVLPLEPLDKAY
      EDDSPGDILAVYAVF--
      >N3613
      LGRGMARTTVEDLETVLNATMDRFAQLPYDGSLQRDDTTAASAPLGTLYGKSPTADMFLQ
      FALESYLDPKRPELGQGLATKAPDALRKRLQLYLGLLSFALEHPTPLLQSLATTLHK-VR
      GKLKNLQREVFARLTASAASGIAHPALTGDMATLMDAVLLMPLLAKLLTIIILEPLDKKY
      SDNSPDDILAAYAAFLS
      >N1689
      MKLGRYRTVQTANEKYLETTAGRYADQNYAGTAQRGVQKANSVPLGTLYPDLPTRDMLLL
      VSLESYLESITAGL-AGLATKAVTLFKVVLVLFLSVTGFALSHPGELFLSMAAVLQTEIR
      GKLKNLTRELLQKLSASLTAGLAVPELTGDEASLGAGKILVPLLAALLVALLLSPLLGGF
      SDDLPNMVLAIYAVTL-
      >N3700
      MKMGRPRTKQSTSQRYLDTAGARYDDQAYAGTLQRGLGNAKGVPLGTLYLDFPIRDMLLL
      VTLESYLESIVAGLYA-GATKAPNLLQAVLILFLNVVGFALLHPGALLLTMAAVLHNELI
      GKLKEFSRELLERLAASVITGLAVPELTGDEGTLAAGVILMALLAALLLYLLLDPLLSGF
      SGDLPDSGLAVHA----
    FASTA
    system bin/"VeryFastTree", "test.fasta"
  end
end
